package com.service;

import com.proto.Method01Reply;
import com.proto.Method01Request;
import com.proto.Service01Grpc;
import com.sql.MySQLServiceGrpc;
import com.sql.SQLRequest;
import com.sql.SQLResponse;
import io.grpc.StatusRuntimeException;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.springframework.stereotype.Service;

/**
 * @author 何永豪
 * @className MySQLClientService
 * @description TODO
 * @date 2023/10/31 17:05
 */
@Service
public class MySQLClientService {

    @GrpcClient("local-grpc-server")
    private MySQLServiceGrpc.MySQLServiceBlockingStub blockingStub;

    public String executeSQL(final String sql) {
        try {
            final SQLResponse sqlResponse = blockingStub.executeSQL(
                    SQLRequest.newBuilder().setSql(sql).build());
            return sqlResponse.getResultList().toString();
        } catch (final StatusRuntimeException e) {
            return "FAILED with " + e.getStatus().getCode().name();
        }
    }
}
